/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.loan;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.loan.Loan;
import src.domain.loan.Loansimulate;
import src.services.gl.GlaccntModel;
import src.services.loan.*;
import src.util.Constanta;
import src.util.Messager;
import src.util.Utils;

/**
 *
 * @author jasuke
 */
@ManagedBean
@SessionScoped
public class LoanInfoBean {

    private List<SelectItem> avaibleCoral, availableGL;
    private List<SelectItem> availableFee, avblPoint;
    private List<Loan> loans;
    private List<Loansimulate> loansimulates;
    
    private Loan loan;
    private LoanModel helper;
    private boolean editMode = true;
    private String filter;
    private String pricipalAmounts, interestAmounts, amounts;
    
    public LoanInfoBean() {
        this.helper = new LoanModel();
    }

    public String getAmounts() {
        return amounts;
    }

    public void setAmounts(String amounts) {
        this.amounts = amounts;
    }

    public String getInterestAmounts() {
        return interestAmounts;
    }

    public void setInterestAmounts(String interestAmounts) {
        this.interestAmounts = interestAmounts;
    }

    public String getPricipalAmounts() {
        return pricipalAmounts;
    }

    public void setPricipalAmounts(String pricipalAmounts) {
        this.pricipalAmounts = pricipalAmounts;
    }

    
    public String getFilter() {
        return filter;
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }
    
    
    public Loan getLoan() {
        return loan;
    }

    public void setLoan(Loan loan) {
        this.loan = loan;
    }

    public List<SelectItem> getAvaibleCoral() {
        return avaibleCoral;
    }

    public List<SelectItem> getAvailableGL() {
        return availableGL;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public List<Loan> getLoans() {
        
        return loans;
    }
    
    public void handleClose(CloseEvent event) {
        this.loan = null;
        this.loansimulates = null;
        
        
    }
    
    public String selectItemValueOf(List<SelectItem> avblList, Integer id) {
        return (String) Utils.selectItemValueOf(avblList, id);
    }
    
    private void initForm() {
        this.avaibleCoral = new CollateralModel().listAll();
        this.availableGL = new GlaccntModel().listAll();
         this.availableFee = new LoanfeeModel().listAll();
        this.avblPoint = new PaypointModel().listAll();
        this.editMode = true;
    }

    public List<Loansimulate> getLoansimulates() {
        return loansimulates;
    }

    public List<SelectItem> getAvailableFee() {
        return availableFee;
    }

    public List<SelectItem> getAvblPoint() {
        return avblPoint;
    }
      
    
    public void openWorksheet() {  
        initForm();
        LoansimulateBean sim = new LoansimulateBean();
        sim.setLoan(this.getLoan());
        sim.simulasi();
        this.loansimulates = sim.getLoansimulates();
        amounts = sim.getAmounts();
        pricipalAmounts = sim.getPricipalAmounts();
        interestAmounts = sim.getInterestAmounts();
    }
    
    public void getInterestRate() {  
        Integer prodId = loan.getLoanprod().getLoanprodId();
        BigDecimal amount = loan.getPrincipalAmount();
        
        if(prodId != null && amount != null) {
            BigDecimal intrate = new LoanprodModel().getInterestRate(loan.getLoanprod().getLoanprodId(), loan.getPrincipalAmount());
            loan.setInterestRate(intrate); 
        } else loan.setInterestRate(null); 
    }  
    
    public void loadLoan() {
        Map<String, Object> savfilter = new HashMap<>();
        //savfilter.put("loanStatus", Constanta.loa);
        this.loans = helper.findAllAdvance(savfilter, " order by loanNo");
    }
    
    public void search() {
        StringBuilder sqlFull = new StringBuilder();
        sqlFull.append("from Loan loan ");                
        sqlFull.append("where closureDate is null ");
        //sqlFull.append("and closedBy is null ");
        
        if (!filter.equals(""))
             sqlFull.append(" and (upper(customer.customerName) like '%").append(filter).append("%' or ").append(" loanNo like '%").append(filter).append("%') ");
        sqlFull.append("order by loanNo");
        this.loans = helper.search(sqlFull.toString());
    }
    
    public void approve(boolean approved, Integer userId) {
        RequestContext context = RequestContext.getCurrentInstance();
        
        if (approved==true) {
            loan.setLoanStatus(Constanta.LOANSTATUS_APPROVED);
            loan.setApprovalUid(userId);
            loan.setApprovalDate(Calendar.getInstance().getTime());
        }
        else loan.setLoanStatus(Constanta.LOANSTATUS_CLOSEDBY_CUSTOMER);
        loan.setConfirmed(true);
        
        boolean success = helper.saveData(loan);
        
        if (success) {
            this.loans.remove(this.loan);
            this.loan = null;
        } 
        
        editMode = !success;        
        Messager.showSaveUpdateStatus(success);     
        context.addCallbackParam("success", success);  
    }
    
    
    
}
